fix: send notifications/cancelled on request timeout and cancellation#2518
Open
weiguangli-io wants to merge 1 commit intomodelcontextprotocol:mainfrom
Open
fix: send notifications/cancelled on request timeout and cancellation#2518weiguangli-io wants to merge 1 commit intomodelcontextprotocol:mainfrom
weiguangli-io wants to merge 1 commit intomodelcontextprotocol:mainfrom
Conversation
Fixes modelcontextprotocol#2507. BaseSession.send_request() never emits a notifications/cancelled message when its in-flight await is interrupted, whether by the SDK's own timeout or by external cancellation. The server never learns the request was abandoned, leaving coroutines suspended holding resources until the session ends. Add a _send_cancelled_notification helper that does best-effort delivery, and call it from both the TimeoutError and CancelledError handlers. The cancellation path uses anyio.CancelScope(shield=True) to ensure the notification is sent even during teardown.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #2507
Root Cause
BaseSession.send_request()never emits anotifications/cancelledmessage when its in-flightawaitis interrupted:anyio.fail_after): raisesMCPErrorwithout notifying the serverCancelledError): propagates without notifying the serverThe server never learns the request was abandoned, so server-side coroutines remain suspended holding resources (DB connections, cursors, locks) until the session ends.
Fix
_send_cancelled_notificationhelper onBaseSessionthat does best-effort delivery (catchesExceptionto avoid masking the original error if the transport is already closed)TimeoutErrorhandler, send the notification before raisingMCPErrorexcept anyio.get_cancelled_exc_class()handler withanyio.CancelScope(shield=True)to ensure the notification is sent even during cancellation teardown, then re-raiseCancelledNotificationis already part of bothClientNotificationandServerNotificationunions, so this works for both client and server sessions.